package admin_log_stash_api

import (
	"blog_go/models"
	"blog_go/models/res"
	"blog_go/plugins/log_stash"
	"blog_go/service/common"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/sirupsen/logrus"
	"time"
)

type LogStashListRsp struct {
	ID        uint            `json:"id"`         // 主键id
	CreatedAt time.Time       `json:"created_at"` // 创建时间 	// 更新时间
	IP        string          `json:"ip"`
	Addr      string          `json:"addr"`
	Level     log_stash.Level `json:"level"`   // 日志等级
	Content   string          `json:"content"` // 日志内容
	UserID    uint            `json:"user_id"` // 登录用户的关联
}

func (AdminLogStashApi) LogStashList(c *gin.Context) {
	page := models.DefaultPageInfo()
	if err := c.ShouldBindQuery(&page); err != nil {
		logrus.Error(err)
		panic(err)
	}
	list, count, err := common.ComList(log_stash.LogStashModel{}, page)
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	var rspList []LogStashListRsp
	for _, model := range list {
		var rsp LogStashListRsp
		err := copier.Copy(&rsp, &model)
		if err != nil {
			logrus.Error(err)
			panic(err)
		}
		rspList = append(rspList, rsp)
	}
	res.OkWithList(rspList, count, c)
}
